﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Final_exam
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Editerform editerform = new Editerform();
            var res = editerform.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var sql = "select * from Students";
                var dt = DbHepler.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("No");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var sql = string.Format("delete from Students where Id={0}", Id);
                var res = DbHepler.AddUpdateordelete(sql);

                if (res == 1)
                {
                    MessageBox.Show("删除成功!");
                    var Stu = "select * from Students";
                    var dt = DbHepler.GetDataTable(Stu);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
            else 
            {
                MessageBox.Show("请选择要删除的数据。");
            }
                
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
                var age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
                var sco = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;

                Editerform editerform = new Editerform(Id, name, age, sco);
                var res = editerform.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var Studentsql = "select * from Students";
                    var dt = DbHepler.GetDataTable(Studentsql);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("No");
                }
            }
            else 
            {
                MessageBox.Show("请选择要修改的数据。");
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;

            var sql = string.Format("select * from Students where Name like '%{0}%'",name);

            var dt = DbHepler.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            var sql = "select * from Students";

            var dt = DbHepler.GetDataTable(sql);

            dataGridView1.DataSource = dt;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
        }
    }
}
